<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<label for="upload">
    <img id="img"  width="150" height="150">
</label>
<input id="upload" type="file" hidden>
</body>
<!-- jQuery文件 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(function () {
        $("#upload").change(function () {
            let form = new FormData();
            form.append("file", $("#upload").prop("files")[0]);
            $.ajax({
                url: "/image/upload",    //后台url
                data: form,
                cache: false,
                async: false,
                type: "POST",          //类型，POST或者GET
                dataType: 'json',       //数据返回类型，可以是xml、json等
                processData: false,
                contentType: false,
                success: function (data) {//成功，回调函数
                    if (data.success) {
                        let url = data.url;
                        $("#img").attr("src", url);
                    } else {
                        alert(data.message);
                    }
                },
                error: function (er) {//失败，回调函数
                    console.log(er)
                    alert(JSON.stringify(data));
                }
            });
        });
    });
</script>
</html>